// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov Date: 14/2/2008
// 16:32:19
// Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for
// new version!
// Decompiler options: packimports(3)
// Source File Name: OrionDeployCommand.java

package br.org.bertol.orion.command;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.evermind.client.orion.AdminCommandException;
import com.evermind.client.orion.AdminCommandUtils;
import com.evermind.server.administration.ApplicationServerAdministrator;

// Referenced classes of package com.evermind.client.orion:
// AdminCommandBase, AdminCommandException, AdminCommandUtils

public class OrionDeployCommand
    extends OrionAdminCommandBase
{

	private String _fileName;
	
	private String _appName;
	
	private Map _extraParams;
	
	private String _clientJarPath;

	public OrionDeployCommand (List args) throws AdminCommandException
	{
		super(args);
		_fileName = null;
		_appName = null;
		_extraParams = new HashMap();
		_clientJarPath = null;
		do
		{
			if (isArgsEmpty()) break;
			String command = getNextArgument("deployment subcommand");
			if (command.equals("-file"))
				_fileName = getNextArgument("deployment -file filename");
			else if (command.equals("-deploymentName"))
				_appName = getNextArgument("deployment name");
			else if (command.equals("-bindWebApp"))
				_extraParams.put("bindWebApp", getNextArgument("web site name for -bindWebApp"));
			else if (!command.equals("-cluster")) if (command.equals("-targetPath"))
				_extraParams.put("targetPath", getNextArgument("-targetPath argument"));
			else if (command.equals("-parent"))
				_extraParams.put("parent", getNextArgument("-parent argument"));
			else if (command.equals("-deploymentDirectory"))
				_extraParams.put("deploymentDirectory", getNextArgument("-deploymentDirectory"));
			else if (command.equals("-autoCreateTables"))
				_extraParams.put("autoCreateTables", getNextArgument("-autoCreateTables"));
			else if (command.equals("-removeEarfile"))
				_extraParams.put("removeEar", "true");
			else if (command.equals("-iiopClientJar"))
			{
				_clientJarPath = getNextArgument("-iiopClientJar");
				_extraParams.put("returnClientJar", "true");
			}
			else
			{
				throw new AdminCommandException("Unknown -deploy switch: " + command, 21);
			}
		}
		while (true);
		boolean donotundeploy = Boolean.getBoolean("DO_NOT_UNDEPLOY_PREVIOUS");
		_extraParams.put("undeployPrevious", donotundeploy ? "false" : "true");
		checkArgIsNull(_fileName, "-file");
		checkArgIsNull(_appName, "-deploymentName");
	}

	public void execute (ApplicationServerAdministrator admin) throws AdminCommandException
	{
		final File packageFile = new File(_fileName);
		
		if (!packageFile.exists())
		    throw new AdminCommandException("Unable to find archive " + _fileName, 22);
		
		if (packageFile.isDirectory())
		    throw new AdminCommandException(_fileName + " is a directory, not an archive.", 23);
		
		Map values = null;
		
		if (getIsLocalHost())
		{
			try
			{
				values = admin.deploy(packageFile.getAbsolutePath(), _appName, _extraParams);
			}
			catch (Exception e)
			{
				throw new AdminCommandException(e, "Deploy error: " + e.getMessage());
			}
		}
		else
		{
			Integer id = AdminCommandUtils.uploadFile(admin, packageFile, _appName + ".ear");
			try
			{
				values = admin.deploy(id, _appName, _extraParams);
			}
			catch (Exception e)
			{
				throw new AdminCommandException(e, "Deploy error: " + e.getMessage());
			}
		}
		System.out.println(values.get("output"));
		
		if (_clientJarPath == null) return;
		
		boolean clientStubsSupported = "true".equals(values.get("<client-stubs-supported>"));
		
		if (!clientStubsSupported)
		    throw new AdminCommandException("The Server does not support generating client stubs",
		        25);
		
		byte clientJarBytes[] = (byte[])values.get("<return-client-jar>");
		
		if (clientJarBytes == null)
		    throw new AdminCommandException(
		        "The Server did not return the iiopClientJar. Please make sure the application deployed contains an EJB jar that requires an associated client jar.",
		        26);
		try
		{
			final FileOutputStream clientJarOutput = new FileOutputStream(_clientJarPath);
			clientJarOutput.write(clientJarBytes);
			clientJarOutput.close();
		}
		catch (Exception e)
		{
			throw new AdminCommandException(e, "Could not make the iiopClientJar" + e.getMessage(),
			    27);
		}
	}
}
